﻿using Solution.Test.Utils;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Security.Principal;
using System.Text;

namespace Solution.Test.utils
{
    class SystemController
    {
        private bool isAdministrator()
        {
            WindowsIdentity identity = WindowsIdentity.GetCurrent();
            WindowsPrincipal principal = new WindowsPrincipal(identity);
            return principal.IsInRole(WindowsBuiltInRole.Administrator);
        }

        public SystemController()
        {
            if (!isAdministrator())
            {
                throw new System.Exception("Program requires Administrator privileges");
            }
        }

        public void DisableAllNetworkAdapters()
        {
            Debug.Print("Attempting to disable all network adapters...");

            List<NetworkAdapter> adapters = NetworkAdapter.GetAllNetworkAdapter();

            Debug.Print("Found {0} adapters", adapters.Count);

            foreach (NetworkAdapter adapter in adapters)
            {
                this.DisableNetworkAdapter(adapter);
            }
        }

        public void EnableNetworkAdapter(NetworkAdapter currentNetworkAdapter)
        {
            // result ={-1: Fail; 0:Unknown; 1:Success}
            int result = -1;

            try
            {
                Debug.Print("Attempting to disable {0}...", currentNetworkAdapter.Name);

                if (currentNetworkAdapter.NetEnabled == -1 && (currentNetworkAdapter.NetConnectionStatus >= 4 && currentNetworkAdapter.NetConnectionStatus <= 7))
                {
                    Debug.Print("Adapter cannot be enabled");
                }
                else
                {
                    result = currentNetworkAdapter.EnableOrDisableNetworkAdapter("Enabled");
                }
            }
            catch (NullReferenceException ex)
            {
                Debug.Print("Failed to construct instance of current network adapter");
                throw new System.ApplicationException("Could not enabled network adapter", ex);
            }

            if (result > 0)
            {
                Debug.Print("Successfully enabled network adapter");
            }
            else
            {
                throw new System.ApplicationException("Could not enable network adapter");
            }
        }

        public void DisableNetworkAdapter(NetworkAdapter currentNetworkAdapter)
        {
            // result ={-1: Fail; 0:Unknown; 1:Success}
            int result = -1;

            try
            {
                Debug.Print("Attempting to disable {0}...", currentNetworkAdapter.Name);

                if(currentNetworkAdapter.NetEnabled == -1)
                {
                    Debug.Print("Adapter is already in a disabled state");
                    return;
                }
                else
                {
                    result = currentNetworkAdapter.EnableOrDisableNetworkAdapter("Disable");
                }
            }
            catch (NullReferenceException ex)
            {
                Debug.Print("Failed to construct instance of current network adapter");
                throw new System.ApplicationException("Could not disable network adapter", ex);
            }

            if (result > 0)
            {
                Debug.Print("Successfully disabled network adapter.");
            }
            else
            {
                throw new System.ApplicationException("Could not disable network adapter");
            }
        }
    }
}
